package com.wang.springboot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wang.springboot.common.Result;

import com.wang.springboot.service.IUserRoleService;
import com.wang.springboot.entity.UserRole;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户和角色关联表 前端控制器
 * </p>
 *
 * @author wy
 * @since 2023-05-23
 */
@RestController
@RequestMapping("/user-role")
public class UserRoleController {

    @Resource
    private IUserRoleService userRoleService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody UserRole userRole) {
        userRoleService.saveOrUpdate(userRole);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        userRoleService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        userRoleService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result findAll() {
        return Result.success(userRoleService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(userRoleService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(userRoleService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

